Ext.define('PJ.view.quotamanagement.AdjustTheQuotaList', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.adjustTheQuotaList'],
    requires: ['AM.default.DefaultSearchForm',
               'AM.default.jsonp.DefaultLoadJsonp'//jsonp加载数据
               ],
   selModel: {
       selType: '',
       // 设置为只能通过复选框选中行
       checkOnly: true
   },
   rec:"",
   cityValue:"",
   countyValue:"",
   isHavePage: false,
    initComponent: function(){
        var me = this;
        me.columns = [{
            header: '单位名称',
            dataIndex: 'deptCode',
            flex: 1,
            minWidth: 80,
            renderer: function(value, meta, record){
            	meta.tdAttr = "data-qtip='" + value + "'";
            	return value;
            }
        },{
            text: '计划名额',//表头合并处
            columns: [{
                text: '计划市级人数',
                dataIndex: 't.cityPlanCount',
                align:"center",
                flex: 1,
                minWidth: 80
            }, {
                text: '计划县级人数',
                dataIndex: 't.countyPlanCount',
                align:"center",
                width: 80
            },{
                header: '小计',
                dataIndex: 't.planCount',
                width: 70,
                align:"center",
                renderer: function(value, meta, record){
                	meta.tdAttr = "data-qtip='" + value + "'";
                	return value;
                }
            }]
        },{
            text: '调整计划名额',//表头合并处
            columns: [{
                text: '调整市级计划人数',
                dataIndex: 't.asignCityCount',
                align:"center",
                flex: 1,
                minWidth: 105,
                renderer: function(value){
            		return "<span style='color:blue;'>"+value+"</span>";
            	},
            	editor:{
            		xtype: 'textfield',
            		regex:/^(0|([1-9]\d{0,2}))$/,
            		regexText: "只允许输入0到999的正整数!",
            		enableKeyEvents:true,
            		listeners: {
            			focus:function(item){
            				var me = this;
            				var record = me.up("grid").getSelectionModel().getLastSelected();
            				this.up('window').down('adjustTheQuotaList').rec = record;
            				this.up('window').down('adjustTheQuotaList').cityValue = record.get('t.asignCityCount');
            			},
            			change: function(item){
            				var me = this;
            				var rec = this.up('window').down('adjustTheQuotaList').rec;
            				var newValue = parseInt(me.getValue());
            				//var record = me.up("grid").getSelectionModel().getLastSelected();
            				var cityValue =  this.up('window').down('adjustTheQuotaList').cityValue;
            				var value = rec.raw.reportedCityCount;
            				var cityPlanCount = rec.get("t.cityPlanCount");
            				var asignCountyCount = rec.get("t.asignCountyCount");
            				var planCount = rec.get("t.planCount");
            				var reportedCityCount =  rec.get("reportedCityCount")+rec.get("reportedCountyCount");
            				/*if(newValue<value){
            					Orchid.alert("提示","调整后的人数不可少于已报市级人数!");
            					me.setValue(cityValue);
            					return;
            				}else if(newValue<cityPlanCount){
            					Orchid.alert("提示","调整后的人数不可少于计划市级人数!");
            					me.setValue(cityValue);
            					return;
            				}else if(planCount>(newValue+asignCountyCount)){
            					Orchid.alert("提示","调整后的人数总计不可少于计划人数总计!");
            					me.setValue(cityValue);
            					return;
            				}else*/ if(newValue>999){
//            					me.setValue(cityValue);
            					Orchid.alert("提示","只允许输入0到999的正整数!",function(){
            						rec.set("t.asignCityCount",cityValue);
            					});
            					return;
            				}else if((newValue+asignCountyCount)<reportedCityCount){
//            					me.setValue(cityValue);
            					Orchid.alert("提示","调整后的人数总计不能低于已报名人数！",function(){
            						rec.set("t.asignCityCount",cityValue);
            					});
            					return;
            				}else{
            					if (!item.isValid()) {
            						return;
            					}
            					var asignCityCount = parseInt(item.getValue());
            					var asignCountyCount = parseInt(rec.get("t.asignCountyCount"));
            					var asignCount = asignCityCount + asignCountyCount;
            					rec.set("t.asignCount",asignCount);
            				}
            			}
            		}
            	}
            }, {
                text: '调整县级计划人数',
                dataIndex: 't.asignCountyCount',
                align:"center",
                width: 105,
                renderer: function(value){
            		return "<span style='color:blue;'>"+value+"</span>";
            	},
            	editor:{
            		xtype: 'textfield',
            		regex:/^(0|([1-9]\d{0,2}))$/,
            		regexText: "只允许输入0到999的正整数!",
            		enableKeyEvents:true,
            		listeners: {
            			focus:function(item){
            				var me = this;
            				var record = me.up("grid").getSelectionModel().getLastSelected();
            				this.up('window').down('adjustTheQuotaList').rec = record;
            				this.up('window').down('adjustTheQuotaList').countyValue = record.get('t.asignCountyCount');
            			},
            			change: function(item){
            				var me = this;
            				var newValue = parseInt(me.getValue());
            				var rec = this.up('window').down('adjustTheQuotaList').rec;
            				//var record = me.up("grid").getSelectionModel().getLastSelected();
            				var countyValue =  this.up('window').down('adjustTheQuotaList').countyValue;
            				var value = rec.raw.reportedCountyCount;
            				var asignCityCount = rec.get("t.asignCityCount");
            				var planCount = rec.get("t.planCount");
            				var reportedCountyCount = rec.get("reportedCityCount")+rec.get("reportedCountyCount");
            				/*if(newValue < value){
            					Orchid.alert("提示","调整后的人数不可少于已报县级人数!");
            					me.setValue(countyValue);
            					return;
            				}else if(planCount>(newValue+asignCityCount)){
            					Orchid.alert("提示","调整后的人数总计不可少于计划人数总计!");
            					me.setValue(countyValue);
            					return;
            				}else */if(newValue>999){
            					Orchid.alert("提示","只允许输入0到999的正整数!",function(){
            						rec.set("t.asignCountyCount",countyValue);
            					});
//            					me.setValue(countyValue);
            					return;
            				}else if((newValue+asignCityCount)<reportedCountyCount){
            					Orchid.alert("提示","调整后的人数总计不能低于已报名人数！",function(){
            						rec.set("t.asignCountyCount",countyValue);
            					});
//            					me.setValue(countyValue);
            					return;
            				}else{
            					if (!item.isValid()) {
            						return;
            					}
            					var asignCountyCount = parseInt(item.getValue());
            					var asignCityCount = parseInt(rec.get("t.asignCityCount"));
            					var asignCount = asignCityCount + asignCountyCount;
            					rec.set("t.asignCount",asignCount);
            				}
            			}
            		}
            	}
            },{
                header: '小计',
                dataIndex: 't.asignCount',
                width: 70,
                align:"center",
                renderer: function(value, meta, record){
                	meta.tdAttr = "data-qtip='" + value + "'";
                	return value;
                }
            }]
        }];
        me.store = Ext.create('PJ.' + prototype + '.quotamanagement.AdjustTheQuotaList', {
            isLoad: false
        });
        //点击可编辑
		this.plugins = Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        });
        me.loadType = gj_onlineLearning_constant.loadType;
        me.callParent(arguments);
    },
    sub:function(){
    	var me = this;
    	var store = me.getStore();
    	me.getSelectionModel().selectAll();
    	var records = me.getSelectionModel().getSelection();
    	var planApplyIds= new Array();
    	var asignCityCounts = new Array();//市级实际报名
		var asignCountyCounts = new Array();//县级实际报名
		Ext.Array.each(records,function(name,index){
			planApplyIds.push(name.get('planApplyId'));
			asignCityCounts.push(name.get('t.asignCityCount'));
			asignCountyCounts.push(name.get('t.asignCountyCount'));
		});
//		var asignCounts= new Array();
//		Ext.Array.each(records,function(name,index){
//			asignCounts.push(name.get('asignCount'));
//		});
		if(gj_onlineLearning_constant.loadType == "jsonp"){
			var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
				loadType: gj_onlineLearning_constant.loadType,
				url: store.adjustmentUrl,
				extraParams: {
//					asignCounts: asignCounts,
					asignCityCounts: asignCityCounts,
					asignCountyCounts: asignCountyCounts,
					planApplyIds:planApplyIds
				}
			});
			store1.load(function(rec, operation, success){
				if(success){
					Ext.Msg.alert('消息', '修改成功！', function(){
						var store = me.getStore();
						me.up('window').close();
						store.load();
					});
				}else{
					Ext.Msg.alert('消息', operation.error, function(){
						me.up('window').close();
					});
				}
			});
		}else{
			Ext.Ajax.request({
				url:store.adjustmentUrl,
				params: {
//					asignCounts: asignCounts,
					asignCityCounts: asignCityCounts,
					asignCountyCounts: asignCountyCounts,
					planApplyIds:planApplyIds
				},
				success:function(response){
					var data=response.responseText;
					var json=Ext.decode(data,true);
					if(json.success){
						Orchid.alert('提示','修改成功',function(){
							me.up('window').close();
							store.load();
						});
					}else{
						Orchid.alert('提示',json.msg,function(){
							store.load();
						});
					}
				}
			});
		}
    },
    listeners: {
        viewready: function(){
        	var me = this;
            // 触发权限过滤事件
            this.fireEvent("permission", this);
            var store = this.store;
            var params = store.proxy.extraParams;
            var newParams = {
            	searchClassId:me.up('window').classId	
            };
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.pageSize = -1;
            store.load();
        },
        afterrender: function(){
            this.loadMask = new Ext.LoadMask(this, {
                msg: "正在加载中,请稍后...."
            });
        }
    }
});
